package com.wlr.study.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/18 15:04
 */
public class LeetCode131 {

    /**
     * 给你一个字符串 s，请你将 s 分割成一些 子串，使每个子串都是 回文串 。返回 s 所有可能的分割方案。
     *
     * @param s
     * @return
     */
    public List<List<String>> partition(String s) {
        List<List<String>> result = new ArrayList<>();
        List<String> path = new ArrayList<>();
        dfs(s, path, result, 0);
        return result;
    }

    private void dfs(String s, List<String> path, List<List<String>> result, int startIndex) {
        if (startIndex == s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isPalindrome(s, startIndex, i)) {
                path.add(s.substring(startIndex, i + 1));
                dfs(s, path, result, i + 1);
                path.remove(path.size() - 1);
            }
        }
    }

    private boolean isPalindrome(String s, int left, int right) {
        while (left < right) {
            if (s.charAt(left++) != s.charAt(right--)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        LeetCode131 leetCode131 = new LeetCode131();

        System.out.println(leetCode131.partition("aab"));
    }

}